package com.chixing.server.cron;

import com.alibaba.fastjson.JSON;
import com.chixing.mapper.CustomerMapper;
import com.chixing.mapper.MyCouponMapper;
import com.chixing.mapper.MyOrderMapper;
import com.chixing.pojo.*;
import com.chixing.service.EmailService;
import com.chixing.util.DateUtil;
import com.chixing.server.sender.Sender;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;

@Component
public class ReturnInformationPush {
    @Autowired
    private MyOrderMapper myOrderMapper;
    @Autowired
    private CustomerMapper customerMapper;
    @Autowired
    private MyCouponMapper myCouponMapper;
    @Autowired
    private MongoTemplate mongoTemplate;
    @Autowired
    private EmailService emailService;
    @Autowired
    private Sender sender;
    @Scheduled(cron = "0 10 12,19,21 * * ?")
//    @Scheduled(cron = "*/10 * * * * ?")
    public void secondPushMessage(){
        MyOrderExample myOrderExample = new MyOrderExample();
        myOrderExample.createCriteria().andReturnCarTimeGreaterThan(new Date());
        List<MyOrder> myOrderList = myOrderMapper.selectByExample(myOrderExample);
        for(MyOrder myOrder : myOrderList){
            long returnCarTime = myOrder.getReturnCarTime().getTime();
            long nowTime = new Date().getTime();
            long oneDay = 24*60*60*1000;
            if(returnCarTime - nowTime < oneDay && returnCarTime - nowTime > 0){
                RequestMessage requestMessage = new RequestMessage();
                requestMessage.setRoom(myOrder.getCustId().toString());
                requestMessage.setUserId("000");
                requestMessage.setType("2");
                requestMessage.setQuestionId("questionId");
                requestMessage.setStatus(0);
                Customer customer = customerMapper.selectByPrimaryKey(myOrder.getCustId());
                if(customer != null) {
                    String custName = "";
                    if(customer.getCustName() != null)
                        custName = customer.getCustName() + "，您好！";
                    requestMessage.setCreateTime(DateUtil.dateToString(new Date(),DateUtil.DATE_PATTEN_YMDHMS));
                    requestMessage.setContent(custName + "请记得在"+ DateUtil.dateToString(myOrder.getReturnCarTime(),DateUtil.DATE_PATTEN_YMDHM) + "还车哦");
                    if(customer.getCustEmail() != null)
                        emailService.sendSimpleMail(customer.getCustEmail(),"小橘租车通知",requestMessage.getContent());
                    mongoTemplate.save(requestMessage);
                    sender.send(JSON.toJSONString(requestMessage));
                    }
                }
        }
        //优惠券过期前2天推送消息给用户
        MyCouponExample myCouponExample = new MyCouponExample();
        MyCouponExample.Criteria criteria = myCouponExample.createCriteria();
        criteria.andMyConuponIneffectiveTimeGreaterThan(new Date()).andStatusEqualTo(2);
        List<MyCoupon> myCoupons = myCouponMapper.selectByExample(myCouponExample);
        for(MyCoupon myCoupon:myCoupons){
            long ineffectiveTime = myCoupon.getMyConuponIneffectiveTime().getTime();
            long nowTime = new Date().getTime();
            long oneDay = 60*60*1000*24*2;
            if(ineffectiveTime - nowTime < oneDay && ineffectiveTime - nowTime > 0){
                RequestMessage requestMessage1 = new RequestMessage();
                requestMessage1.setRoom(myCoupon.getCustId().toString());
                requestMessage1.setUserId("111");
                requestMessage1.setType("2");
                requestMessage1.setQuestionId("questionId");
                requestMessage1.setStatus(0);
                Customer customer = customerMapper.selectByPrimaryKey(myCoupon.getCustId());
                if(customer != null){
                    String custName = customer.getCustName() + ",您好!";
                    requestMessage1.setCreateTime(DateUtil.dateToString(new Date(),DateUtil.DATE_PATTEN_YMDHMS));
                    requestMessage1.setContent(custName + "您的优惠券将在两天之内到期失效，快去使用吧！");
                    emailService.sendSimpleMail(customer.getCustEmail(),"小橘租车通知",requestMessage1.getContent());
                }
            }
        }
    }
}
